package com.ymttest.business.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymatou.activity.a.service.ApproveProductCall;
import com.ymatou.activity.a.service.LoginResultCall;
import com.ymatou.evt.sellerservice.service.ApplyActivityProductCall;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.JsonBeanFile;

import cn.ymatou.activityadmin.service.SaveActivityCall;

public class ActivityCallService {
	
	
	
	
	
	/***
	 * 获得用户cookie
	 * @param nameStr
	 * @param pswdStr
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static List<String> getLoginCookieList(String nameStr,String pswdStr) throws ClientProtocolException, IOException{
		LoginResultCall loginResultCall = new LoginResultCall();
		loginResultCall.setData("ret=&sign=&LoginName="+nameStr+"&PassWord="+pswdStr);
		loginResultCall.callService();
		
		return loginResultCall.getCookies();
		
		
	}
	
	/***
	 * 活动审核通过
	 * @param cookieList
	 * @param productInActivityId
	 * @param activityId
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws JSONException
	 */
	public static JSONObject approveProductInActivity(List<String> cookieList,String productInActivityId,String activityId) throws ClientProtocolException, IOException, JSONException{
		ApproveProductCall approveProductCall = new ApproveProductCall();
		
		JSONObject approveProductBean = JsonBeanHelper.getJsonBean(JsonBeanFile.ApproveProductInActivity);
		JsonBeanHelper.updateJsonBean(approveProductBean,"productInActivityId", productInActivityId);
		JsonBeanHelper.updateJsonBean(approveProductBean,"activityId", activityId);
		approveProductCall.setData(approveProductBean);
		
		approveProductCall.setCookies(cookieList);
		approveProductCall.callService();
		
		//验证审核通过
		return new JSONObject(approveProductCall.getReturnData());
	}
	
	
	/***
	 * 商品参加活动 指定单规格
	 * @param sellerId
	 * @param productId
	 * @param catalogId
	 * @param activityId
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws JSONException
	 */
	public static JSONObject applyProductInActivity(int sellerId,String productId,String catalogId,String activityId) throws ClientProtocolException, IOException, JSONException{
		ApplyActivityProductCall applyActivityProductCall = new ApplyActivityProductCall();
		JSONObject applyEffectiveActBean = JsonBeanHelper.getJsonBean(JsonBeanFile.ApplyProductInActivity);
		
		Map<String, Object> updateMap = new HashMap<String, Object>();
		
		updateMap.put("UserId", sellerId);
		updateMap.put("ActivityStockList[0]->UserId",sellerId);
		
		updateMap.put("ProductId",productId);
		updateMap.put("ActivityStockList[0]->ProductId",productId);
		
		updateMap.put("ActivityId", activityId);
		updateMap.put("ActivityStockList[0]->ActivityId",activityId);
		
		updateMap.put("ActivityStockList[0]->CatalogId",catalogId);
		
		JsonBeanHelper.updateJsonBean(applyEffectiveActBean, updateMap);
		
		applyActivityProductCall.setData(applyEffectiveActBean);
		applyActivityProductCall.callService();
		
		return new JSONObject(applyActivityProductCall.getReturnData());
		
	}
	
	/***
	 * 默认该商品下的所有catalogId都报名参加活动
	 * @param sellerId
	 * @param productId
	 * @param activityId
	 * @param updateMap
	 * @return
	 * @throws Exception
	 */
	public static JSONObject applyProductInActivity(int sellerId,String productId,String activityId,Map<String,Object> updateMap) throws Exception{
		//updateMap do nothing
		
		List<YmtCatalogsI> ymtCatalogsIList = new YmtCatalogsIWapper().selectCatalogsByProductId(productId);
		JSONObject applyEffectiveActBean = JsonBeanHelper.getJsonBean(JsonBeanFile.ApplyProductInActivity);
		
		if (ymtCatalogsIList.size()>0){
			Map<String, Object> requestMap = new HashMap<String, Object>();
			requestMap.put("UserId", sellerId);
			requestMap.put("ProductId",productId);
			requestMap.put("ActivityId", activityId);
			requestMap.put("ActivityStockList[0]->UserId",sellerId);
			requestMap.put("ActivityStockList[0]->ProductId",productId);
			requestMap.put("ActivityStockList[0]->ActivityId",activityId);
			requestMap.put("ActivityStockList[0]->CatalogId",ymtCatalogsIList.get(0).getsCatalogId());
			requestMap.put("ActivityStockList[0]->Sku",ymtCatalogsIList.get(0).getsSKU());
			JsonBeanHelper.updateJsonBean(applyEffectiveActBean, requestMap);
			
			for(int i=1;i<ymtCatalogsIList.size();i++){
				JSONArray CatalogListBean = applyEffectiveActBean.getJSONArray("ActivityStockList");
				applyEffectiveActBean.getJSONArray("ActivityStockList").put(i, new JSONObject(CatalogListBean.get(0).toString()));
				
				applyEffectiveActBean.getJSONArray("ActivityStockList").getJSONObject(i).put("UserId",sellerId);
				applyEffectiveActBean.getJSONArray("ActivityStockList").getJSONObject(i).put("ProductId",productId);
				applyEffectiveActBean.getJSONArray("ActivityStockList").getJSONObject(i).put("ActivityId",activityId);
				applyEffectiveActBean.getJSONArray("ActivityStockList").getJSONObject(i).put("Sku",ymtCatalogsIList.get(i).getsSKU());
				applyEffectiveActBean.getJSONArray("ActivityStockList").getJSONObject(i).put("CatalogId",ymtCatalogsIList.get(i).getsCatalogId());
			}
		}
		ApplyActivityProductCall applyActivityProductCall = new ApplyActivityProductCall();
		applyActivityProductCall.setData(applyEffectiveActBean);
		applyActivityProductCall.callService();
		return new JSONObject(applyActivityProductCall.getReturnData());
	}
	
	
	
	
	/***
	 * 指定规格报名
	 * @param sellerId
	 * @param productId
	 * @param activityId
	 * @param CatalogList
	 * @return
	 * @throws Exception
	 */
	public static JSONObject applyProductInActivity(int sellerId,String productId,String activityId,List<String> CatalogList) throws Exception{
		//updateMap do nothing
		
		
		JSONObject applyEffectiveActBean = JsonBeanHelper.getJsonBean(JsonBeanFile.ApplyProductInActivity);
		
		List<YmtCatalogsI> ymtCatalogsIList =new ArrayList<>();
		YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
		CatalogList.forEach(catalog->{
			try {
				ymtCatalogsIList.add(ymtCatalogsIWapper.selectCatalogsByCatalogId(catalog));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});
		
		if (ymtCatalogsIList.size()>0){
			Map<String, Object> requestMap = new HashMap<String, Object>();
			requestMap.put("UserId", sellerId);
			requestMap.put("ProductId",productId);
			requestMap.put("ActivityId", activityId);
			requestMap.put("ActivityStockList[0]->UserId",sellerId);
			requestMap.put("ActivityStockList[0]->ProductId",productId);
			requestMap.put("ActivityStockList[0]->ActivityId",activityId);
			requestMap.put("ActivityStockList[0]->CatalogId",ymtCatalogsIList.get(0).getsCatalogId());
			requestMap.put("ActivityStockList[0]->Sku",ymtCatalogsIList.get(0).getsSKU());
			JsonBeanHelper.updateJsonBean(applyEffectiveActBean, requestMap);
			
			for(int i=1;i<ymtCatalogsIList.size();i++){
				JSONArray CatalogListBean = applyEffectiveActBean.getJSONArray("ActivityStockList");
				applyEffectiveActBean.getJSONArray("ActivityStockList").put(i, new JSONObject(CatalogListBean.get(0).toString()));
				
				applyEffectiveActBean.getJSONArray("ActivityStockList").getJSONObject(i).put("UserId",sellerId);
				applyEffectiveActBean.getJSONArray("ActivityStockList").getJSONObject(i).put("ProductId",productId);
				applyEffectiveActBean.getJSONArray("ActivityStockList").getJSONObject(i).put("ActivityId",activityId);
				applyEffectiveActBean.getJSONArray("ActivityStockList").getJSONObject(i).put("Sku",ymtCatalogsIList.get(i).getsSKU());
				applyEffectiveActBean.getJSONArray("ActivityStockList").getJSONObject(i).put("CatalogId",ymtCatalogsIList.get(i).getsCatalogId());
			}
		}
		ApplyActivityProductCall applyActivityProductCall = new ApplyActivityProductCall();
		applyActivityProductCall.setData(applyEffectiveActBean);
		applyActivityProductCall.callService();
		return new JSONObject(applyActivityProductCall.getReturnData());
	}
	
	
	
	
	
	
	/***
	 * 报名正在进行活动
	 * @param updateMap
	 * @return
	 * @throws JSONException
	 */
	
	public static int createIsolationActivityInProcess() throws JSONException{
		return createActivityInProcess(MapUtil.hashMap("ActivityTemplateId",2));
	}
	
	public static int createNoIsolationActivityInProcess() throws JSONException{
		return createActivityInProcess(MapUtil.hashMap("ActivityTemplateId",1));
	}
	
	
	public static int createActivityInProcess() throws JSONException{
		return createActivityInProcess(MapUtil.hashMap());
	}
	
	
	
	public static int createActivityInProcess(Map<String, Object> updateMap) throws JSONException{
		
		Map<String, Object> dateUpdateMap = MapUtil.hashMap();
		dateUpdateMap.put("BeginTime", YMTDateUtil.getBeforeOrNextMonth(-1));//活动开始时间
		dateUpdateMap.put("EndTime", YMTDateUtil.getBeforeOrNextMonth(2));//活动结束时间
		dateUpdateMap.put("ApplyDeadline", YMTDateUtil.getBeforeOrNextMonth(1));//活动报名截止时间
		
		if (updateMap!=null){dateUpdateMap.putAll(updateMap);}
		
		return createActivity(dateUpdateMap);
		
	}
	
	
	/***
	 * 报名正未来活动
	 * @param updateMap
	 * @return
	 * @throws JSONException
	 */
	public static int createActivityInFuture() throws JSONException{
		return createActivityInFuture(MapUtil.hashMap());
	}
	
	public static int createActivityInFuture(Map<String, Object> updateMap) throws JSONException{
		Map<String, Object> dateUpdateMap = MapUtil.hashMap();
		dateUpdateMap.put("BeginTime", YMTDateUtil.getBeforeOrNextMonth(3));//活动开始时间
		dateUpdateMap.put("EndTime", YMTDateUtil.getBeforeOrNextMonth(5));//活动结束时间
		dateUpdateMap.put("ApplyDeadline", YMTDateUtil.getBeforeOrNextMonth(4));//活动报名截止时间
		
		if (updateMap!=null){dateUpdateMap.putAll(updateMap);}
		
		return createActivity(dateUpdateMap);
		
	}
	
	
	
	public static List<Integer> createActivityInFuture(int activityCount, Map<String, Object> updateMap) throws JSONException{
		List<Integer> activityIds = new ArrayList<>();
		
		for(int i=0;i<activityCount;i++){
			Map<String, Object> dateUpdateMap = MapUtil.hashMap();
			dateUpdateMap.put("BeginTime", YMTDateUtil.getBeforeOrNextMonth(4*(i+1)));//活动开始时间
			dateUpdateMap.put("EndTime", YMTDateUtil.getBeforeOrNextMonth(4*(i+1)+2));//活动结束时间
			dateUpdateMap.put("ApplyDeadline", YMTDateUtil.getBeforeOrNextMonth(4*(i+1)+1));//活动报名截止时间
			
			if (updateMap!=null){dateUpdateMap.putAll(updateMap);}
			
			activityIds.add(createActivity(dateUpdateMap));
		}
		
		return activityIds;
		
	}
	
	
	
	
	private static int createActivity(Map<String, Object> updateMap)throws JSONException{
		int activityId = 0;
		//有限取活动池
		/*int ActivityTemplateId = Integer.parseInt(updateMap.get("ActivityTemplateId").toString());
		activityId = ActivityPoolService.getActivityFromPool(ActivityTemplateId);
		if (activityId>0){
			return activityId;
		}*/
		
		
		SaveActivityCall saveActivityCall= new SaveActivityCall();
		saveActivityCall.setCookies(AdminCallService.getLoginCookieList());
		
		
		Map<String, Object> allUpdateMap = MapUtil.hashMap();
		if (updateMap!=null){allUpdateMap.putAll(updateMap);}
		
		
		JSONObject ret =  saveActivityCall.callAndGetReturnData(allUpdateMap);
		activityId = ret.getJSONObject("Data").getInt("Rows");
		//ActivityPoolService.setActivityFromPool(ActivityTemplateId, activityId);
		return activityId;
	}
	
	
	public static void main(String[] args) throws Exception{
		System.out.println(createActivityInProcess());
	}
	
	
	
}
